package com.c.ctgproject.wiget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.constraintlayout.utils.widget.ImageFilterView
import com.c.ctgproject.R
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.handlerText
import com.c.ctgproject.utils.textSet
import kotlinx.android.synthetic.main.item_doorkeeper_material_out_list.view.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.imageResource
import java.math.BigDecimal

class NumberAddSubLayout(context: Context, attributeSet: AttributeSet? = null) : LinearLayoutCompat(context, attributeSet) {
    //    private var binding: ViewNumberAddSubLayoutBinding? = null
    private var mNumberET: EditText? = null
    private var mSubIV: ImageFilterView? = null
    private var mAddIV: ImageFilterView? = null
    private var listener: NumberAddSubLayoutListener? = null

    fun addListener(listener: NumberAddSubLayoutListener){
        this.listener=listener
    }

    init {
        val root = LayoutInflater.from(context).inflate(R.layout.view_number_add_sub_layout, this, true)
        mNumberET = root.findViewById(R.id.mNumberET)
        mNumberET?.textSet("1")
        mSubIV = root.findViewById(R.id.mSubIV)
        mAddIV = root.findViewById(R.id.mAddIV)
        mAddIV?.setOnClickListener {
            handlerNumber(true)
        }
        mSubIV?.setOnClickListener {
            handlerNumber(false)
        }
        mNumberET?.handlerText {
            when {
                it.isEmpty() -> {
                    mSubIV?.imageResource = R.drawable.icon_sub_uncircel
                    mSubIV?.isEnabled = false
                }
                BigDecimal(it) < BigDecimal("1") -> {
                    mSubIV?.imageResource = R.drawable.icon_sub_uncircel
                    mSubIV?.isEnabled = false
                }
                BigDecimal(it) >= BigDecimal("1") -> {
                    mSubIV?.imageResource = R.drawable.icon_sub_circle_canclick
                    mSubIV?.isEnabled = true
                }
            }
            if (it.isNotEmpty()) {
                mNumberET?.setSelection(it.length)
            }
        }
        mNumberET?.setOnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                if (mNumberET?.text.handlerNull().isEmpty()) {
                    mNumberET?.textSet("0")
                }
            }
        }
    }

    fun textSet(number: String) {
        if (number.isEmpty()||number=="null") {
            mNumberET?.textSet("0")
        } else {
            mNumberET?.textSet(number)
        }
    }

    fun textSet(number: Int) {
        mNumberET?.textSet(number.handlerNull())
    }

    fun text(): String {
        return mNumberET?.text.handlerNull()
    }

    private fun handlerNumber(add: Boolean) {
        if (mNumberET?.text.handlerNull().isEmpty()) {
            mNumberET?.textSet("0")
        }

        var currentNumber = BigDecimal(mNumberET?.text.handlerNull())
        currentNumber = if (add) {
            currentNumber.add(BigDecimal("1"))
        } else {
            currentNumber.subtract(BigDecimal("1"))
        }
        mNumberET?.textSet(currentNumber.handlerNull())

//        if (currentNumber < BigDecimal("1")) {
//            mSubIV?.imageResource = R.drawable.icon_sub_uncircel
//            mSubIV?.isEnabled = false
//
//        } else {
//            mSubIV?.imageResource = R.drawable.icon_sub_circle_canclick
//            mSubIV?.isEnabled = true
//        }
        listener?.onCountChange(currentNumber.handlerNull())
    }

    interface NumberAddSubLayoutListener {
        fun onCountChange(count: String)
    }
}